<?php 
namespace app\user\controller;
use app\common\traits\controller\TraitController;
/**
 * 心愿订单
 */
class WishOrder extends \think\Controller
{
	use TraitController;
	//押金
	const DEPOSIT = 200;
	//订单状态
	const OrderNotPay = 100;//订单未付款
	//订单来源
	const ORDERSOURCE = 3;//心愿订单
	/**
	 * 心愿订单房车资源详情
	 */
	public function getWishCaravanDetail(){
		//房车id
		$carvanId = input('get.cid/d');
		//心愿id
		$wishId = input('get.wid/d');
		//判断该房车是否为接该单用户的房车
		$carvan = model('Caravans')->getCaravanInfo($carvanId);
		$wishData = model('Wishs')->hasWhere('users',['meet_user_id'=>$carvan->user_id, 'wish_id' => $wishId])->where(['user_id' => self::getLoginUserInfo('id')])->find();
		if(!$wishData){
			$this->error('该房车不存在');
			return;
		}
		
		$share = model('Calendar')->getCaravanStatus($carvanId);
		$this->assign('share',$share['shares']);
		//入住时间
		$start_date=strtotime($wishData['start_date']);$end_date=strtotime($wishData['end_date']);
		$days = ($end_date-$start_date)/(24*60*60)+1;
        $this->assign('days',$days);
		//押金
		$this->assign('deposit',self::DEPOSIT);
		//房车信息
		$carInfo = model('Caravans')->where('id',$carvanId)->find();
		$picCaravan = $this->getEscapeSlash($carInfo->piccaravan);
		$this->assign('picCaravan',$picCaravan);
		//房车封面
        $caravancover= $this->getEscapeSlash($carInfo->caravancover);
        $this->assign('caravancover',$caravancover);
		//房车所在营地旅行基数
		$travel_money_base = model('Camps')->where('id',$carInfo['services'])->value('travel_money_base');
		$this->assign('travel_money_base',$travel_money_base);
		//获取总旅行币
		$total_intergral = model('Orders')->getTotalCoin($wishData['start_date'],$wishData['end_date'],$travel_money_base);
		//加上悬赏后的总旅行币
		$all_intergral = $total_intergral + $wishData['score'];
		$this->assign('all_intergral',$all_intergral);
		return view('wishorder/wish_order',['carInfo'=>$carInfo,'wishData'=>$wishData,'total_intergral'=>$total_intergral]);	
	}



	/**
	 * 下订单界面
	 */
	public function getSubmitOrder(){
		//房车id
		$carvanId = input('get.cid/d');
		//心愿id
		$wishId = input('get.wid/d');
		//判断该房车是否为接该单用户的房车
		$carvan = model('Caravans')->getCaravanInfo($carvanId);
		$wishData = model('Wishs')->hasWhere('users',['meet_user_id'=>$carvan->user_id, 'wish_id' => $wishId])->where(['user_id' => self::getLoginUserInfo('id')])->find();
		if(!$wishData){
			$this->error('该房车不存在');
			return;
		}
		
		//入住时间
		$start_date=strtotime($wishData['start_date']);$end_date=strtotime($wishData['end_date']);
		$days = ($end_date-$start_date)/(24*60*60)+1;
        $this->assign('days',$days);
		
		//获取联系人信息
		$contacts = model('ContactInfos')->where('user_id',self::getLoginUserInfo('id'))->select();
		$this->assign('contacts',$contacts);
		
		//会员旅行币余额
        $integral = model('Users')->where('id',self::getLoginUserInfo('id'))->value('integral');
        $this->assign('integral',$integral);
		//房车信息
		$carInfo = model('Caravans')->where('id',$carvanId)->find();
		$picCaravan = $this->getEscapeSlash($carInfo->piccaravan);
		$this->assign('picCaravan',$picCaravan);
		//房车所在营地旅行基数
		$travel_money_base = model('Camps')->where('id',$carInfo['services'])->value('travel_money_base');
		 $this->assign('travel_money_base',$travel_money_base);
		//获取总旅行币
		$total_intergral = model('Orders')->getTotalCoin($wishData['start_date'],$wishData['end_date'],$travel_money_base);
		//加上悬赏后的总旅行币
		$all_intergral = $total_intergral + $wishData['score'];
		//押金
		$this->assign('deposit',self::DEPOSIT);
		return view('wishorder/submit_order',['wishData'=>$wishData,'carInfo'=>$carInfo,'total_intergral'=>$total_intergral,'all_intergral'=>$all_intergral]);
	}

	/**
	 * 生成订单
	 */
	public function postSaveOrder(){
		//房车id
		$carvanId = input('post.cid/d');
		//心愿id
		$wishId = input('post.wid/d');
		//判断该房车是否为接该单用户的房车
		$carvan = model('Caravans')->getCaravanInfo($carvanId);
		$wishData = model('Wishs')->hasWhere('users',['meet_user_id'=>$carvan->user_id, 'wish_id' => $wishId])->where(['user_id' => self::getLoginUserInfo('id')])->find();
		if(!$wishData){
			$this->error('该房车不存在');
			return;
		}
		 //判断是否同意
        $Protocol=input('post.Protocol/b');
        if ($Protocol!==true) {
            return $this->error('请同意协议',$url = null,'',$wait=5);
        }
		
        //图片验证码
		$code=input('post.imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($code)) {
            return $this->error(lang('图片验证码错误'),$url = null,'',$wait=5);
        }
		
		//房车所在营地信息
		$campData = model('Camps')->where('id',$carvan['services'])->find();
		//获取总旅行币
		$intergral = model('Orders')->getTotalCoin($wishData['start_date'],$wishData['end_date'],$campData['travel_money_base']);
		//加上悬赏后的旅行币
		$total_intergral = $intergral + $wishData['score'];
		//添加联系人
        $post_info['user_id'] = self::getLoginUserInfo('id');;
        $post_info['name'] = input('post.real_name');
        $post_info['phone'] = input('post.phone');
        $post_info['email'] = input('post.email');
        $post_info['type_number'] = input('post.id_card');
        //增加必须的联系人
        if (empty(input('post.rent_contact_id/a'))&&empty(input('post.name'))&&empty(input('post.phone'))) {
            return $this->error('请填写联系方式',$url = null,'',$wait=5);
        }
		
		//存入订单信息
		$array_info['number'] = model('Orders')->createOrderId();
		$array_info['rent_user_id'] = self::getLoginUserInfo('id');
		$array_info['rent_contact_id'] = input('post.rent_contact_id/a');
		$array_info['status'] = self::OrderNotPay;
		$array_info['source'] = self::ORDERSOURCE;
		$array_info['caravan_id'] = $carvanId;
		$array_info['camp_id'] = $campData['id'];
		$array_info['service_fee'] = 0;
		$array_info['deposit'] = self::DEPOSIT;
		$array_info['total_intergral'] = $total_intergral;
		//悬赏的钱数
		$array_info['total_money'] = $wishData['offer'];
		$array_info['total_amount'] = self::DEPOSIT + $wishData['offer'];
		$array_info['take_car_area'] = $campData['address'];
		$array_info['leave_car_area'] = $campData['address'];
		$array_info['take_car_date'] = $wishData['start_date'];
		$array_info['leave_car_date'] = $wishData['end_date'];
		$array_info['caravan_user_id'] = $carvan['user_id'];
		$array_info['rent_user_account'] = self::getLoginUserInfo('mobile');
		//生成订单
		$result = model('Wishs')->createOrder($wishData['id'],$carvan['user_id'],$array_info);
		if($result){
			//联系人信息入库
			if (!empty($post_info)&&$post_info['phone']) {
				$post_info['type']=input('post.type');
            	model('ContactInfos')->createContactInfo($post_info);
			}

			model('Messages')->createMsg($msg_info = ['users_id' => $array_info['caravan_user_id'], 'body' => '恭喜您,发布心愿的会员确认了您的接单请求!']);

			$this->redirect("/wishorders/pay?number=".$array_info['number']."&wid=".$wishData['id']);
		}
		$this->error('订单失败');
	}

	/**
	 * 支付界面 
	 */
	 public function getPay(){
	 	//心愿id
	 	$this->assign('wid',input('get.wid/d'));
	 	$number = input('get.number');
        $post_order_info = model('Orders')->where('number',$number)->find();
		if(!$post_order_info){
			$this->error('订单错误');
		}
		$contacts = [];
		//联系人id
        $rent_contact_id = $post_order_info->rent_contact_id;//halt($post_order_info);
        if (!empty($rent_contact_id)) {
            foreach ($rent_contact_id as $k => $v) {
                $list = model('ContactInfos')->where('id',$v)->find();
                $contacts[] = $list;
            }
        }
		$this->assign('contacts',$contacts);
        return view('wishorder/pay',['post_order_info'=>$post_order_info]);
	}
	 
	 
	/**
	 * 取消订单
	 */
	public function postCancelOrder(){
		$num = input('post.num');
		$wid = input('post.wid');
		model('Orders')->cancelWishOrder($num,$wid);		
	} 
	/**
	 * 转移路径斜杠
	 */
	public function getEscapeSlash($data_arr)
    {
        $data = str_replace("\\","/",$data_arr);
        return $data;
    }
}







 ?>